/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.utils;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.sys.dao.SysMenuSwitchDao;
import com.thinkgem.jeesite.modules.sys.entity.SysMenuSwitch;

import java.util.Map;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2013-5-29
 */
public class SwitchUtils{
	
	private static SysMenuSwitchDao sysMenuSwitchDao = SpringContextHolder.getBean(SysMenuSwitchDao.class);

	private static final String CACHE_SWITCH_MAP = "switchMap";
	
	public static Boolean isClose(String permission){
		Map<String, Boolean> switchMap = getSwitchMap();
		return switchMap.get(permission);
	}
	
	private static Map<String, Boolean> getSwitchMap(){
		@SuppressWarnings("unchecked")
//		Map<String, Boolean> switchMap = (Map<String, Boolean>)CacheUtils.get(CACHE_SWITCH_MAP);
//		if (switchMap==null){
			Map<String, Boolean> switchMap = Maps.newHashMap();
			for (SysMenuSwitch menuSwitch : sysMenuSwitchDao.findAllList(new SysMenuSwitch())){
				switchMap.put(menuSwitch.getPermission(), SysMenuSwitch.DEL_FLAG_DELETE.equals(menuSwitch.getDelFlag()));
			}
//			CacheUtils.put(CACHE_SWITCH_MAP, switchMap);
//		}
		return switchMap;
	}

    public static void clearAll(){
        CacheUtils.removeAll(CACHE_SWITCH_MAP);
    }
}
